﻿
#include"MeshSerialize.hpp"

namespace MMF { namespace Mesh {


    MMFSerialize::SerialInBuffer& MeshSerialize::Serialize(MMFSerialize::SerialInBuffer& buff, const TesMesh& mesh)
    {
        //序列化顶点
        size_t size = mesh.vertices.size();
        buff >> size;
        for (size_t i = 0; i < size; i++)
        {
            buff >> mesh.vertices[i].x;
            buff >> mesh.vertices[i].y;
            buff >> mesh.vertices[i].z;
        }

        //序列化颜色
        size = mesh.colors.size();
        buff >> size;
        for (size_t i = 0; i < size; i++)
        {
            buff >> mesh.colors[i].x;
            buff >> mesh.colors[i].y;
            buff >> mesh.colors[i].z;
        }

        //序列化法线
        size = mesh.normals.size();
        buff >> size;
        for (size_t i = 0; i < size; i++)
        {
            buff >> mesh.normals[i].x;
            buff >> mesh.normals[i].y;
            buff >> mesh.normals[i].z;
        }

        //序列化三角形
        size = mesh.triangles.size();
        buff >> size;
        for (size_t i = 0; i < size; i++)
        {
            buff >> mesh.triangles[i];
        }

        //序列化纹理坐标
        size = mesh.uv.size();
        buff >> size;
        for (size_t i = 0; i < size; i++)
        {
            buff >> mesh.uv[i].x;
            buff >> mesh.uv[i].y;
        }

        return buff;
    }

    MMFSerialize::SerialOutBuffer& MeshSerialize::Deserialize(MMFSerialize::SerialOutBuffer& buff, TesMesh& mesh)
    {
        //反序列化定点
        size_t size = 0;
        buff << size;
        mesh.vertices.resize(size);
        for (size_t i = 0; i < size; i++)
        {
            buff << mesh.vertices[i].x;
            buff << mesh.vertices[i].y;
            buff << mesh.vertices[i].z;
        }

        //反序列化颜色
        buff << size;
        mesh.colors.resize(size);
        for (size_t i = 0; i < size; i++)
        {
            buff << mesh.colors[i].x;
            buff << mesh.colors[i].y;
            buff << mesh.colors[i].z;
        }

        //反序列化法线
        buff << size;
        mesh.normals.resize(size);
        for (size_t i = 0; i < size; i++)
        {
            buff << mesh.normals[i].x;
            buff << mesh.normals[i].y;
            buff << mesh.normals[i].z;
        }

        //反序列化三角形
        buff << size;
        mesh.triangles.resize(size);
        for (size_t i = 0; i < size; i++)
        {
            buff << mesh.triangles[i];
        }

        //反序列化纹理坐标
        buff << size;
        mesh.uv.resize(size);
        for (size_t i = 0; i < size; i++)
        {
            buff << mesh.uv[i].x;
            buff << mesh.uv[i].y;
        }

        return buff;
    }

}}